﻿Class MainWindow
    Public Sub New()
        InitializeComponent()
        ' 确保TextBox在初始化后已加载
        AddHandler txt_test.Loaded, AddressOf TextBoxLoaded
    End Sub

    Private Sub TextBoxLoaded(sender As Object, e As RoutedEventArgs)
        ' 设置TextBox的样式以确保可以接收拖放
        Dim textBox = DirectCast(sender, TextBox)
        textBox.Background = Brushes.White ' 确保背景不为空
        textBox.Focusable = True ' 确保可聚焦
    End Sub

    Private Sub txt_test_DragEnter(sender As Object, e As DragEventArgs) Handles txt_test.DragEnter
        ' 检查拖放数据格式并设置允许的操作
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effects = DragDropEffects.Copy
        Else
            e.Effects = DragDropEffects.None
        End If
        e.Handled = True ' 标记事件已处理
    End Sub

    Private Sub txt_test_Drop(sender As Object, e As DragEventArgs) Handles txt_test.Drop
        ' 处理拖放的文件
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
            If files IsNot Nothing AndAlso files.Length > 0 Then
                txt_test.Text = files(0) ' 显示第一个文件路径
            End If
        End If
        e.Handled = True ' 标记事件已处理
    End Sub
End Class